<?php
/**
 * Created by PhpStorm.
 * User: gaojie
 * Date: 2018/11/16
 * Time: 10:09 AM
 */

namespace app\Http\Controllers\Frontend;


use App\Http\Controllers\Controller;
use App\Model\Device;
use App\Model\MeteorologicalStation;
use App\Model\MSDeviceRelation;
use App\Model\Site;
use Illuminate\Http\Request;

class WeatherController extends Controller
{

    public function getShow(){
        $user = $this->_getCurrentUser();
        $site = Site::where('user_id',$user->id)->first();
        $met_station = MeteorologicalStation::where('site_id',$site->id)->get();
        if(count($met_station)>0){
            return view('frontend.weather.station-lists',['lists'=>$met_station]);
        }else{
            $devices = Device::where('user_id',$user->id)->get();
            return view('frontend.weather.add-station',['devices'=>$devices]);
        }
    }

    public function getStationsList(){
        $user = $this->_getCurrentUser();
        $site = Site::where('user_id',$user->id)->first();
        $met_station = MeteorologicalStation::where('site_id',$site->id)->get();
        return  $this->json_success($met_station);
    }

    public function getAddStation(){
        $user = $this->_getCurrentUser();
        $devices = Device::where('user_id',$user->id)->get();
        return view('frontend.weather.add-station',['devices'=>$devices]);
    }

    public function postCreateStation(Request $request){
        $param['name'] = $request->input('name');
        $param['direction'] = $request->input('direction');
        $param['remarks'] = $request->input('remarks');
        $param['devices'] = json_encode($request->input('devices'));
        $user = $this->_getCurrentUser();
        $site = Site::where('user_id',$user->id)->first();
        $param['site_id'] = $site?$site->id:false;
        if(!$param['site_id']) {
            return $this->json_error('找不到你的电站','40001');
        }

        $station = MeteorologicalStation::create($param);

        if($station){
            return $this->json_success('成功');
        }else{
            return $this->json_error('失败','40001');
        }
    }

    public function postDelStation(Request $request){
        $param['station_id'] = $request->input('delete_id');
        $station = MeteorologicalStation::destroy($param);
        if($station){
            return $this->json_success('成功');
        }
        return $this->json_error('失败','40001');
    }

    public function getUpdateStation(Request $request){
        $id = $request->input('update_id');
//        dump($id);
        $ms = MeteorologicalStation::find($id);
        $selected = json_decode($ms->devices);
        $devices  = Device::where('site_id',$ms->site_id)->get();
        $checks = [];
        $unchecks = [];
        foreach ($devices as $device){
            $id = $device->id;
            $id = strval($id);
            if(in_array($id,$selected)){
                array_push($checks,['name'=>$device->name,'id'=>$id]);
            }else{
                array_push($unchecks,['name'=>$device->name,'id'=>$id]);
            }
        }
        return view('frontend.weather.update-station',['ms'=>$ms,'checks'=>$checks,'unchecks'=>$unchecks]);
    }

    public function postUpdateStation(Request $request){
        $param = $request->all();
//        dump($param);
        $devices = json_encode($param['devices']);
        $obj = MeteorologicalStation::find($param['id']);
        $result = $obj->update(['name'=>$param['name'],'direction'=>$param['direction'],'remarks'=>$param['remarks'],'devices'=>$devices]);
        if($result){
            return $this->json_success();
        }else{
            return $this->json_error('错误','4001');
        }
    }

}